package cn.kp.learning.controller;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.kp.learning.model.CoreUserOrg;
import cn.kp.learning.model.CoreUserOrgExample;
import cn.kp.learning.model.CoreUserOrgExample.Criteria;
import cn.kp.learning.service.CoreUserOrgService;
import cn.kp.learning.util.ResponseUtil;
@Controller
@RequestMapping("/userOrgCon")
public class UserOrganizationController {
	
	@Resource
	private CoreUserOrgService coreUserOrgService;
	
	//USER-ORG ADD
	@RequestMapping("/addUserOrg")
	public void addUserOrg(HttpServletResponse response,HttpServletRequest request, CoreUserOrg cuo) throws Exception{
		JSONObject json=new JSONObject();
		System.out.println("user-org add :" + cuo.getOrgCompanyId());
		int orgaddresult = coreUserOrgService.addCuo(cuo);
		System.out.println("user-org Action : "+orgaddresult);
		if(orgaddresult>0){
			json.put("success", true);
		}else{
			json.put("success", false);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//USER-ORG DELETE
	@RequestMapping("/deleteUserOrg")
	public void deleteUserOrg(HttpServletResponse response,HttpServletRequest request, CoreUserOrg cuo ){
		System.out.println("user organization and roles get ... " + cuo.getAllName());
	}
	//USER-ORG UPDATE
	@RequestMapping("/updateUserOrg")
	public void userOrgUpdate(HttpServletResponse response,HttpServletRequest request, CoreUserOrg cuo) throws Exception{
		JSONObject json=new JSONObject();
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("userName", request.getParameter("userName"));
		map.put("passWord", request.getParameter("password"));
		
		System.out.println("adding person : " + cuo.getAllName() +" - " + cuo.getUserName());
		//user judge ...
		/*People pl = peopleService.getPeople(map);
		if(null!= pl){
			System.out.println(pl.getUserName() +" is login ..." );
		}*/
		// check before add
		//pExample
		Integer peopleUpdateResult = coreUserOrgService.updateCuo(cuo);
		System.out.println("user update ....[Class:UserOrganizationController] + [Method:updateUserOrgModify]");
		if(peopleUpdateResult>0){
			//int addResult = peopleService.updatePeople(pl);
			System.out.println("update result : " + peopleUpdateResult);
			//get user menus ...
			
			//get user roles ...
			
			//get user groups ...
			
			//set user session ... 
	
			json.put("success", true);
		}else{
			//modify user is not existed ...
			json.put("isExist", true);
			json.put("success", false);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println("get people size " + "1");
		//return lt;
	}
	//USER-ORG SELECT-lIST
	@RequestMapping("/getUserOrgList")
	public void getUserOrgList(HttpServletResponse response,HttpServletRequest request){
		System.out.println("get people list ... ");
		String personUserName = request.getParameter("userName");
		System.out.println("get para : " + request.getParameter("userName"));
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));
		
		List<CoreUserOrg> lt = null;
		CoreUserOrgExample example = new CoreUserOrgExample();
		Criteria cc = example.createCriteria();
		cc.andUserNameEqualTo(personUserName);
		String startRows = request.getParameter("start");
		String pagesLimit = request.getParameter("limit");
		if(null!=startRows && !startRows.equals("")){
			example.setStartRow(Integer.parseInt(startRows));
		}
		if(null!=pagesLimit && !pagesLimit.equals("")){
			example.setPageSize(Integer.parseInt(startRows) + Integer.parseInt(pagesLimit));
		}
		String search1 = request.getParameter("search1");
		String search2 = request.getParameter("search2");
		if(null!=search1 && !search1.equals("")){
			cc.andAllNameLike("%"+request.getParameter("search1") +"%");
		}
		if(null!=search2 && !search2.equals("")){
			cc.andUserNameLike("%"+request.getParameter("search2") + "%");
		}
		
		lt = coreUserOrgService.getCuoList(example);
		int countPeople = coreUserOrgService.countCuo(example);
		
		JSONObject json = new JSONObject();
		//
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("pages", lt.size()/10+1);
		json.put("total", countPeople);
		json.put("rows", jsonArray);
		
		System.out.println("User-Org Json : " + json.toString());
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("get User-Org size " + lt.size());
	}
	//USER-ORG SELECT-SINGLE
	@RequestMapping("/getUserOrg")
	public void getUserOrg(HttpServletResponse response,HttpServletRequest request){
		System.out.println("get people list ... ");
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));
		
	}	
	
}